package com.xjrsoft.module.erpModel.caseErpCustomer.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.annotation.XjrLog;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.utils.RedisUtil;
import com.xjrsoft.module.erpModel.caseErpCustomer.dto.*;
import com.xjrsoft.module.erpModel.caseErpCustomer.entity.CaseErpCustomer;
import com.xjrsoft.module.erpModel.caseErpCustomer.entity.CaseErpCustomerGather;
import com.xjrsoft.module.erpModel.caseErpCustomer.service.ICaseErpCustomerGatherService;
import com.xjrsoft.module.erpModel.caseErpCustomer.service.ICaseErpCustomerService;
import com.xjrsoft.module.erpModel.caseErpCustomer.vo.CaseErpCustomerInfoVo;
import com.xjrsoft.module.erpModel.caseErpSupplier.dto.CaseErpSupplierPageDto;
import com.xjrsoft.module.organization.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户回款【case_erp_customer_gather】 前端控制器
 * </p>
 *
 * @author hnyyzy
 * @since 2023-07-12
 */
@RestController
@RequestMapping(GlobalConstant.CASE_ERP_CUSTOMER +"/caseErpCustomerGather")
@Api(value = GlobalConstant.CASE_ERP_CUSTOMER +"/caseErpCustomerGather", tags = "客户回款")
@AllArgsConstructor
public class CaseErpCustomerGatherController {
    private ICaseErpCustomerGatherService caseErpCustomerGatherService;

    private ICaseErpCustomerService caseErpCustomerService;

    private RedisUtil redisUtil;

    @GetMapping(value = "/page")
    @ApiOperation("客户回款分页")
    public R page(CaseErpCustomerGatherDto dto) {
        return R.ok(caseErpCustomerGatherService.getPageList(dto));
    }

    @PostMapping
    @ApiOperation(value = "新增客户回款信息")
    public R add(@Valid @RequestBody AddCaseErpCustomerGatherDto dto) {
        CaseErpCustomerGather caseErpCustomerGather = BeanUtil.toBean(dto, CaseErpCustomerGather.class);
        CaseErpCustomer byId = caseErpCustomerService.getById(dto.getCustomerId());
        if (StrUtil.isNotBlank(byId.getName())){
            caseErpCustomerGather.setName(byId.getName());
        }
        caseErpCustomerGather.setAlreadyAmount(BigDecimal.ZERO);
        caseErpCustomerGather.setUnpaidAmount(dto.getWaitAmount());
        List<User> allUser = redisUtil.get(GlobalConstant.USER_CACHE_KEY, new TypeReference<List<User>>() {
        });
        String allUserIdStr = StrUtil.join(StringPool.COMMA, dto.getPrincipalIds());
        List<Long> ids = Arrays.stream(allUserIdStr.split(StringPool.COMMA)).map(Convert::toLong).collect(Collectors.toList());
        List<String> names = allUser.stream().filter(x -> ids.contains(x.getId())).map(User::getName).collect(Collectors.toList());
        caseErpCustomerGather.setPrincipalNames(StrUtil.join(StringPool.COMMA,names));
        caseErpCustomerGatherService.save(caseErpCustomerGather);
        return R.ok(true);
    }

    @PutMapping
    @ApiOperation(value = "修改客户回款信息")
    public R update(@Valid @RequestBody UpdateCaseErpCustomerGatherDto dto) {
        CaseErpCustomerGather caseErpCustomerGather = BeanUtil.toBean(dto, CaseErpCustomerGather.class);
        CaseErpCustomer byId = caseErpCustomerService.getById(dto.getCustomerId());
        if (StrUtil.isNotBlank(byId.getName())){
            caseErpCustomerGather.setName(byId.getName());
        }
        List<User> allUser = redisUtil.get(GlobalConstant.USER_CACHE_KEY, new TypeReference<List<User>>() {
        });
        String allUserIdStr = StrUtil.join(StringPool.COMMA, dto.getPrincipalIds());
        List<Long> ids = Arrays.stream(allUserIdStr.split(StringPool.COMMA)).map(Convert::toLong).collect(Collectors.toList());
        List<String> names = allUser.stream().filter(x -> ids.contains(x.getId())).map(User::getName).collect(Collectors.toList());
        caseErpCustomerGather.setPrincipalNames(StrUtil.join(StringPool.COMMA,names));
        return R.ok(caseErpCustomerGatherService.updateById(caseErpCustomerGather));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "根据id查询客户回款信息")
    public R info(@RequestParam Long id) {
        CaseErpCustomerGather caseErpCustomerGather = caseErpCustomerGatherService.getById(id);
        if (caseErpCustomerGather == null) {
            R.error("找不到此客户回款信息！");
        }
        return R.ok(caseErpCustomerGather);
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @XjrLog(value = "删除客户回款信息")
    public R delete(@Valid @RequestBody Long id) {
        return R.ok(caseErpCustomerGatherService.removeById(id));
    }


    @GetMapping(value = "/gather-detail-info")
    @ApiOperation(value = "根据客户回款id查询客户回款记录信息")
    public R gatherDetailInfo(@RequestParam Long id) {
        return R.ok(caseErpCustomerGatherService.gatherDetailInfo(id));
    }

}
